//
//  LTUpgradePopView.m
//  ASMMessage
//
//  Created by apple on 2024/6/18.
//

#import "LTUpgradePopView.h"
#import "UIView+LayoutMethods.h"

typedef NS_ENUM(NSInteger, GradientColorDirection) {
    GradientColorDirectionLevel, // 水平渐变
    GradientColorDirectionVertical, // 竖直渐变
    GradientColorDirectionDownDiagonalLine, // 向上对角线渐变
    GradientColorDirectionUpwardDiagonalLine, // 向下对角线渐变
};

@interface LTUpgradePopView()
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIImageView *logoImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) UIButton *ignoreButton;
@property (nonatomic, strong) UIButton *updateButton;
@property (nonatomic, assign) BOOL isForce;
@property (nonatomic, copy) dispatch_block_t sureHandler;
@property (nonatomic, copy) dispatch_block_t ignoreHandler;
@end

@implementation LTUpgradePopView

+ (void)showWithForce:(BOOL)isForce title:(NSString *)title content:(NSString *)content sureHandler:(dispatch_block_t)sureHandler ignoreHandler:(dispatch_block_t)ignoreHandler {
    LTUpgradePopView *popView = [[LTUpgradePopView alloc] initWithFrame:UIScreen.mainScreen.bounds isForce:isForce title:title content:content sureHandler:sureHandler ignoreHandler:ignoreHandler];
    UIView *superView = [UIApplication sharedApplication].delegate.window;
    if (!superView) {
        superView = [UIApplication sharedApplication].windows.lastObject;
    }
    [superView addSubview:popView];
}

- (instancetype)initWithFrame:(CGRect)frame isForce:(BOOL)isForce title:(NSString *)title content:(NSString *)content sureHandler:(dispatch_block_t)sureHandler ignoreHandler:(dispatch_block_t)ignoreHandler {
    if (self = [super initWithFrame:frame]) {
        self.isForce = isForce;
        _sureHandler = sureHandler;
        _ignoreHandler = ignoreHandler;
        self.titleLabel.text = [NSString stringWithFormat:@"更新版本 V%@", title];
        self.textView.text = [content stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];
        [self setupSubviews];
        [self addConstraints];
        [self show];
    }
    return self;
}

- (void)setupSubviews {
    [self addSubview:self.bgView];
    [self.bgView addSubview:self.logoImageView];
    [self.bgView addSubview:self.titleLabel];
    [self.bgView addSubview:self.textView];
    [self.bgView addSubview:self.ignoreButton];
    [self.bgView addSubview:self.updateButton];
    self.backgroundColor = rgba(0, 0, 0, 0.8);
}

- (void)addConstraints {
    self.bgView.frame = CGRectMake(15, 0, self.width - 30, 0);
    self.logoImageView.frame = CGRectMake((self.bgView.width - 76) * 0.5, 30, 76, 86);
    self.titleLabel.frame = CGRectMake(30, self.logoImageView.bottom + 30, self.bgView.width - 60, 32);
    self.textView.frame = CGRectMake(30, self.titleLabel.bottom + 15, self.bgView.width - 60, 50);
    CGSize textSize = [self sizeForFont:self.textView.font size:CGSizeMake(self.textView.width, CGFLOAT_MAX) text:self.textView.text];
    if (textSize.width < self.textView.width) {
        self.textView.width = textSize.width + 2;
        self.textView.centerX = self.logoImageView.centerX;
    }
    self.textView.height = textSize.height;
    if (self.textView.height < 50) {
        self.textView.height = 50;
    }
    if (textSize.height > 88) {
        self.textView.height = 88;
    }
    if (self.isForce) {
        self.updateButton.frame = CGRectMake(30, self.textView.bottom + 30, self.bgView.width - 60, 40);
        self.ignoreButton.hidden = YES;
    }else {
        CGFloat btnW = (self.bgView.width - 75) * 0.5;
        self.ignoreButton.frame = CGRectMake(25, self.textView.bottom + 30, btnW, 40);
        self.updateButton.frame = CGRectMake(self.ignoreButton.right + 20, self.textView.bottom + 30, btnW, 40);
    }
    self.updateButton.backgroundColor = [self gradientColorWithSize:self.updateButton.bounds.size direction:GradientColorDirectionLevel startColor:rgba(41, 241, 156, 1) endColor:rgba(2, 161, 249, 1)];
    self.bgView.height = self.updateButton.bottom + 20;
    self.bgView.top = (self.height - self.bgView.height) * 0.5;
}

- (void)show {
    self.backgroundColor = rgba(0, 0, 0, 0.0);
    [UIView animateWithDuration:0.25 animations:^{
        self.backgroundColor = rgba(0, 0, 0, 0.8);
    }];
}

- (void)ignoreButtonClick {
    if (self.ignoreHandler) {
        self.ignoreHandler();
    }
    [UIView animateWithDuration:0.25 animations:^{
        self.alpha = 0;
    }];
}

- (void)updateButtonClick {
    if (self.sureHandler) {
        self.sureHandler();
    }
}

- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc] initWithFrame:CGRectZero];
        _bgView.backgroundColor = [UIColor whiteColor];
        _bgView.layer.cornerRadius = 20;
        _bgView.layer.masksToBounds = YES;
    }
    return _bgView;
}

- (UIImageView *)logoImageView {
    if (!_logoImageView) {
        _logoImageView = [UIImageView new];
        _logoImageView.image = [UIImage imageNamed:@"version_top_icon"];
    }
    return _logoImageView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.textColor = rgb(51, 51, 51);
    }
    return _titleLabel;
}

- (UITextView *)textView {
    if (!_textView) {
        _textView = [[UITextView alloc] init];
        _textView.contentInset = UIEdgeInsetsZero;
        _textView.textContainerInset = UIEdgeInsetsZero;
        _textView.textContainer.lineFragmentPadding = 0;
        _textView.editable = NO;
        _textView.textColor = rgba(142, 142, 142, 1);
        _textView.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
    }
    return _textView;
}

- (UIButton *)updateButton {
    if (!_updateButton) {
        _updateButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_updateButton setTitle:@"立即更新" forState:UIControlStateNormal];
        _updateButton.layer.cornerRadius = 20;
        _updateButton.layer.masksToBounds = YES;
        [_updateButton addTarget:self action:@selector(updateButtonClick) forControlEvents:UIControlEventTouchUpInside];
        _updateButton.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
        [_updateButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
    return _updateButton;
}

- (UIButton *)ignoreButton {
    if (!_ignoreButton) {
        _ignoreButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_ignoreButton setTitle:@"暂不更新" forState:UIControlStateNormal];
        _ignoreButton.layer.cornerRadius = 20;
        _ignoreButton.layer.borderColor = rgba(142, 142, 142, 1).CGColor;
        _ignoreButton.layer.borderWidth = 0.5;
        _ignoreButton.layer.masksToBounds = YES;
        [_ignoreButton addTarget:self action:@selector(ignoreButtonClick) forControlEvents:UIControlEventTouchUpInside];
        _ignoreButton.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
        [_ignoreButton setTitleColor:rgba(51, 51, 51, 1) forState:UIControlStateNormal];
    }
    return _ignoreButton;
}

- (UIColor *)gradientColorWithSize:(CGSize)size direction:(GradientColorDirection)direction startColor:(UIColor *)startColor endColor:(UIColor *)endColor {
    
    if (CGSizeEqualToSize(size, CGSizeZero) || !startColor || !endColor) {
        return nil;
    }
    
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = CGRectMake(0, 0, size.width, size.height);
    
    CGPoint startPoint = CGPointMake(0.0, 0.0);
    if (direction == GradientColorDirectionUpwardDiagonalLine) {
        startPoint = CGPointMake(0.0, 1.0);
    }
    
    CGPoint endPoint = CGPointMake(0.0, 0.0);
    switch (direction) {
        case GradientColorDirectionVertical:
            endPoint = CGPointMake(0.0, 1.0);
            break;
        case GradientColorDirectionDownDiagonalLine:
            endPoint = CGPointMake(1.0, 1.0);
            break;
        case GradientColorDirectionUpwardDiagonalLine:
            endPoint = CGPointMake(1.0, 0.0);
            break;
        default:
            endPoint = CGPointMake(1.0, 0.0);
            break;
    }
    gradientLayer.startPoint = startPoint;
    gradientLayer.endPoint = endPoint;
    
    gradientLayer.colors = @[(__bridge id)startColor.CGColor, (__bridge id)endColor.CGColor];
    UIGraphicsBeginImageContext(size);
    [gradientLayer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return [UIColor colorWithPatternImage:image];
}

- (CGSize)sizeForFont:(UIFont *)font size:(CGSize)size text:(NSString *)text {
    NSMutableDictionary *attr = [[NSMutableDictionary alloc] init];
    attr[NSFontAttributeName] = font;
    return [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attr context:nil].size;
}

@end
